﻿using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Http;
using WeatherHistory.Shared.Constants;

namespace WeatherHistory.Shared.Helpers;

public static class SecurityHelper
{
    public static string ComputeSha256Hash(string rawData)
    {
        using var sha256Hash = SHA256.Create();
        var inputBytes = Encoding.UTF8.GetBytes(rawData);
        var hashBytes = sha256Hash.ComputeHash(inputBytes);

        return Convert.ToHexString(hashBytes);
    }

    public static void AuthenticateWithCookie(HttpContext httpContext, long accountId)
    {
        httpContext.Response.Cookies.Append("id", accountId.ToString(), new CookieOptions
        {
            Path = "/",
            Expires = null,
            Secure = false,
            SameSite = SameSiteMode.Lax,
            HttpOnly = false,
            MaxAge = null
        });
    }
}